<template>
  <div class="comments-container">
    <div class="page-header">
      <h1>评论管理</h1>
      <div class="header-actions">
        <button class="action-btn delete-btn" :disabled="selectedComments.length === 0" @click="batchDelete">
          <i class="fa fa-trash"></i>
          批量删除
        </button>
      </div>
    </div>
    
    <!-- 搜索和筛选 -->
    <div class="search-filter">
      <div class="search-box">
        <i class="fa fa-search"></i>
        <input 
          v-model="searchQuery" 
          type="text" 
          placeholder="搜索评论内容、用户名或视频标题"
          @input="handleSearch"
        />
      </div>
      <div class="filter-group">
        <select v-model="filterStatus" @change="handleFilter">
          <option value="">全部状态</option>
          <option value="approved">已通过</option>
          <option value="pending">待审核</option>
          <option value="rejected">已拒绝</option>
          <option value="deleted">已删除</option>
        </select>
        <select v-model="sortBy" @change="handleSort">
          <option value="time_desc">时间倒序</option>
          <option value="time_asc">时间正序</option>
          <option value="likes_desc">点赞最多</option>
          <option value="likes_asc">点赞最少</option>
        </select>
      </div>
    </div>
    
    <!-- 评论列表 -->
    <div class="comments-list">
      <div v-for="comment in paginatedComments" :key="comment.id" class="comment-card">
        <div class="comment-header">
          <div class="comment-author">
            <img :src="comment.authorAvatar" alt="用户头像" class="author-avatar" />
            <div class="author-info">
              <span class="author-name">{{ comment.authorName }}</span>
              <span class="comment-time">{{ formatDate(comment.createdTime) }}</span>
            </div>
          </div>
          <div class="comment-meta">
            <span class="comment-status" :class="comment.status">
              {{ getStatusText(comment.status) }}
            </span>
            <span class="comment-likes"><i class="fa fa-thumbs-up"></i> {{ comment.likes }}</span>
          </div>
        </div>
        
        <div class="comment-content">
          <p>{{ comment.content }}</p>
        </div>
        
        <div class="comment-video">
          <img :src="comment.videoThumbnail" alt="视频缩略图" class="video-thumbnail" />
          <div class="video-info">
            <span class="video-title">{{ comment.videoTitle }}</span>
          </div>
        </div>
        
        <div class="comment-actions">
          <button v-if="comment.status === 'pending'" class="action-btn approve-btn" @click="approveComment(comment)">
            <i class="fa fa-check"></i>
            通过
          </button>
          <button v-if="comment.status === 'pending'" class="action-btn reject-btn" @click="rejectComment(comment)">
            <i class="fa fa-times"></i>
            拒绝
          </button>
          <button class="action-btn delete-btn" @click="deleteComment(comment)">
            <i class="fa fa-trash"></i>
            删除
          </button>
          <button class="action-btn view-btn" @click="viewCommentDetails(comment)">
            <i class="fa fa-eye"></i>
            查看详情
          </button>
        </div>
      </div>
      
      <!-- 空状态 -->
      <div v-if="filteredComments.length === 0" class="empty-state">
        <i class="fa fa-comment"></i>
        <p>暂无评论数据</p>
      </div>
    </div>
    
    <!-- 分页 -->
    <div v-if="filteredComments.length > 0" class="pagination">
      <button :disabled="currentPage === 1" @click="currentPage = 1">
        <i class="fa fa-angle-double-left"></i>
      </button>
      <button :disabled="currentPage === 1" @click="currentPage--">
        <i class="fa fa-angle-left"></i>
      </button>
      <span class="page-info">
        第 {{ currentPage }} 页，共 {{ totalPages }} 页，总计 {{ filteredComments.length }} 条评论
      </span>
      <button :disabled="currentPage === totalPages" @click="currentPage++">
        <i class="fa fa-angle-right"></i>
      </button>
      <button :disabled="currentPage === totalPages" @click="currentPage = totalPages">
        <i class="fa fa-angle-double-right"></i>
      </button>
    </div>
    
    <!-- 评论详情对话框 -->
    <div v-if="showCommentDetails" class="dialog-overlay" @click="closeCommentDetails">
      <div class="dialog details-dialog" @click.stop>
        <div class="dialog-header">
          <h3>评论详情</h3>
          <button class="close-button" @click="closeCommentDetails">
            <i class="fa fa-times"></i>
          </button>
        </div>
        <div v-if="selectedComment" class="dialog-content">
          <div class="comment-detail">
            <div class="detail-section">
              <h4>评论信息</h4>
              <div class="detail-item">
                <span class="detail-label">评论ID：</span>
                <span class="detail-value">{{ selectedComment.id }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">评论时间：</span>
                <span class="detail-value">{{ formatDate(selectedComment.createdTime) }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">最后更新：</span>
                <span class="detail-value">{{ formatDate(selectedComment.updatedTime) }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">评论状态：</span>
                <span class="detail-value status-badge" :class="selectedComment.status">
                  {{ getStatusText(selectedComment.status) }}
                </span>
              </div>
            </div>
            
            <div class="detail-section">
              <h4>用户信息</h4>
              <div class="detail-item">
                <span class="detail-label">用户名：</span>
                <span class="detail-value">{{ selectedComment.authorName }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">用户ID：</span>
                <span class="detail-value">{{ selectedComment.authorId }}</span>
              </div>
            </div>
            
            <div class="detail-section">
              <h4>视频信息</h4>
              <div class="detail-item">
                <span class="detail-label">视频标题：</span>
                <span class="detail-value">{{ selectedComment.videoTitle }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">视频ID：</span>
                <span class="detail-value">{{ selectedComment.videoId }}</span>
              </div>
            </div>
            
            <div class="detail-section">
              <h4>评论内容</h4>
              <div class="comment-content-full">
                {{ selectedComment.content }}
              </div>
            </div>
          </div>
          
          <div class="dialog-footer">
            <button class="cancel-button" @click="closeCommentDetails">关闭</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CommentsView',
  data() {
    return {
      // 模拟评论数据
      comments: [
        {
          id: 'comment1',
          content: '这个视频太棒了，学到了很多有用的知识！',
          authorId: 'user1',
          authorName: '张三',
          authorAvatar: '/src/assets/logo.png',
          videoId: 'video1',
          videoTitle: 'Vue3 入门教程',
          videoThumbnail: '/src/assets/科技数码展宣传图.jpg',
          status: 'approved',
          likes: 45,
          createdTime: '2024-01-15T10:30:00Z',
          updatedTime: '2024-01-15T10:30:00Z',
        },
        {
          id: 'comment2',
          content: '讲解很详细，就是节奏有点慢，希望下次能加快节奏。',
          authorId: 'user2',
          authorName: '李四',
          authorAvatar: '/src/assets/logo.png',
          videoId: 'video1',
          videoTitle: 'Vue3 入门教程',
          videoThumbnail: '/src/assets/科技数码展宣传图.jpg',
          status: 'approved',
          likes: 12,
          createdTime: '2024-01-15T11:15:00Z',
          updatedTime: '2024-01-15T11:15:00Z',
        },
        {
          id: 'comment3',
          content: '感谢分享，期待更多精彩内容！',
          authorId: 'user3',
          authorName: '王五',
          authorAvatar: '/src/assets/logo.png',
          videoId: 'video2',
          videoTitle: '美食制作：意式肉酱面',
          videoThumbnail: '/src/assets/意式肉酱面.jpg',
          status: 'approved',
          likes: 34,
          createdTime: '2024-01-15T14:20:00Z',
          updatedTime: '2024-01-15T14:20:00Z',
        },
        {
          id: 'comment4',
          content: '按照这个方法做了，味道确实不错！推荐大家尝试。',
          authorId: 'user4',
          authorName: '赵六',
          authorAvatar: '/src/assets/logo.png',
          videoId: 'video2',
          videoTitle: '美食制作：意式肉酱面',
          videoThumbnail: '/src/assets/意式肉酱面.jpg',
          status: 'approved',
          likes: 56,
          createdTime: '2024-01-15T16:45:00Z',
          updatedTime: '2024-01-15T16:45:00Z',
        },
        {
          id: 'comment5',
          content: '这款游戏真的很好玩，画面精美，玩法丰富！',
          authorId: 'user5',
          authorName: '孙七',
          authorAvatar: '/src/assets/logo.png',
          videoId: 'video3',
          videoTitle: '黑悟空游戏实况',
          videoThumbnail: '/src/assets/黑悟空.jpg',
          status: 'approved',
          likes: 78,
          createdTime: '2024-01-15T18:30:00Z',
          updatedTime: '2024-01-15T18:30:00Z',
        },
        {
          id: 'comment6',
          content: '最新的剧情太精彩了，期待下一集！',
          authorId: 'user6',
          authorName: '周八',
          authorAvatar: '/src/assets/logo.png',
          videoId: 'video4',
          videoTitle: '鬼灭之刃最新话分析',
          videoThumbnail: '/src/assets/鬼灭之刃.jpeg',
          status: 'approved',
          likes: 92,
          createdTime: '2024-01-16T09:10:00Z',
          updatedTime: '2024-01-16T09:10:00Z',
        },
        {
          id: 'comment7',
          content: '这里有个错误，正确的做法应该是...',
          authorId: 'user7',
          authorName: '吴九',
          authorAvatar: '/src/assets/logo.png',
          videoId: 'video1',
          videoTitle: 'Vue3 入门教程',
          videoThumbnail: '/src/assets/科技数码展宣传图.jpg',
          status: 'pending',
          likes: 0,
          createdTime: '2024-01-16T10:20:00Z',
          updatedTime: '2024-01-16T10:20:00Z',
        },
        {
          id: 'comment8',
          content: '垃圾视频，浪费时间！',
          authorId: 'user8',
          authorName: '郑十',
          authorAvatar: '/src/assets/logo.png',
          videoId: 'video2',
          videoTitle: '美食制作：意式肉酱面',
          videoThumbnail: '/src/assets/意式肉酱面.jpg',
          status: 'rejected',
          likes: 0,
          createdTime: '2024-01-16T11:30:00Z',
          updatedTime: '2024-01-16T11:30:00Z',
        },
      ],
      searchQuery: '',
      filterStatus: '',
      sortBy: 'time_desc',
      currentPage: 1,
      itemsPerPage: 5,
      selectedComments: [],
      showCommentDetails: false,
      selectedComment: null,
    }
  },
  computed: {
    filteredComments() {
      let filtered = [...this.comments]
      
      // 搜索
      if (this.searchQuery) {
        const query = this.searchQuery.toLowerCase()
        filtered = filtered.filter(comment => 
          comment.content.toLowerCase().includes(query) || 
          comment.authorName.toLowerCase().includes(query) || 
          comment.videoTitle.toLowerCase().includes(query),
        )
      }
      
      // 状态筛选
      if (this.filterStatus) {
        filtered = filtered.filter(comment => comment.status === this.filterStatus)
      }
      
      // 排序
      filtered.sort((a, b) => {
        switch (this.sortBy) {
          case 'time_asc':
            return new Date(a.createdTime) - new Date(b.createdTime)
          case 'time_desc':
            return new Date(b.createdTime) - new Date(a.createdTime)
          case 'likes_asc':
            return a.likes - b.likes
          case 'likes_desc':
            return b.likes - a.likes
          default:
            return new Date(b.createdTime) - new Date(a.createdTime)
        }
      })
      
      return filtered
    },
    paginatedComments() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.filteredComments.slice(start, end)
    },
    totalPages() {
      return Math.ceil(this.filteredComments.length / this.itemsPerPage)
    },
  },
  methods: {
    handleSearch() {
      this.currentPage = 1
      this.selectedComments = []
    },
    handleFilter() {
      this.currentPage = 1
      this.selectedComments = []
    },
    handleSort() {
      this.currentPage = 1
    },
    formatDate(dateString) {
      const date = new Date(dateString)
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
      })
    },
    getStatusText(status) {
      const statusMap = {
        'approved': '已通过',
        'pending': '待审核',
        'rejected': '已拒绝',
        'deleted': '已删除',
      }
      return statusMap[status] || '未知'
    },
    approveComment(comment) {
      if (confirm(`确定要通过评论 "${comment.content.substring(0, 20)}..." 吗？`)) {
        // 在实际应用中，这里应该调用API
        comment.status = 'approved'
      }
    },
    rejectComment(comment) {
      if (confirm(`确定要拒绝评论 "${comment.content.substring(0, 20)}..." 吗？`)) {
        // 在实际应用中，这里应该调用API
        comment.status = 'rejected'
      }
    },
    deleteComment(comment) {
      if (confirm(`确定要删除评论 "${comment.content.substring(0, 20)}..." 吗？此操作不可撤销！`)) {
        // 在实际应用中，这里应该调用API
        const index = this.comments.findIndex(c => c.id === comment.id)
        if (index > -1) {
          this.comments[index].status = 'deleted'
          // 从选中列表中移除
          this.selectedComments = this.selectedComments.filter(id => id !== comment.id)
        }
      }
    },
    batchDelete() {
      if (confirm(`确定要删除选中的 ${this.selectedComments.length} 条评论吗？此操作不可撤销！`)) {
        // 在实际应用中，这里应该调用API
        this.comments.forEach(comment => {
          if (this.selectedComments.includes(comment.id)) {
            comment.status = 'deleted'
          }
        })
        this.selectedComments = []
      }
    },
    viewCommentDetails(comment) {
      this.selectedComment = comment
      this.showCommentDetails = true
    },
    closeCommentDetails() {
      this.showCommentDetails = false
      this.selectedComment = null
    },
  },
}
</script>

<style scoped>
.comments-container {
  max-width: 1000px;
  margin: 0 auto;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h1 {
  color: #2c3e50;
  margin: 0;
  font-size: 24px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

/* 搜索和筛选 */
.search-filter {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  gap: 20px;
  flex-wrap: wrap;
}

.search-box {
  position: relative;
  flex: 1;
  min-width: 300px;
}

.search-box i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #95a5a6;
}

.search-box input {
  width: 100%;
  padding: 10px 15px 10px 40px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
}

.filter-group {
  display: flex;
  gap: 10px;
}

.filter-group select {
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  background: white;
}

/* 评论列表 */
.comments-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-bottom: 20px;
}

.comment-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  transition: transform 0.3s, box-shadow 0.3s;
}

.comment-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(0,0,0,0.15);
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.comment-author {
  display: flex;
  align-items: center;
  gap: 10px;
}

.author-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
}

.author-name {
  font-weight: 600;
  color: #2c3e50;
}

.comment-time {
  font-size: 12px;
  color: #95a5a6;
  margin-left: 10px;
}

.comment-meta {
  display: flex;
  align-items: center;
  gap: 15px;
}

.comment-status {
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.comment-status.approved {
  background: #2ecc71;
  color: white;
}

.comment-status.pending {
  background: #f39c12;
  color: white;
}

.comment-status.rejected {
  background: #e74c3c;
  color: white;
}

.comment-status.deleted {
  background: #95a5a6;
  color: white;
}

.comment-likes {
  font-size: 14px;
  color: #3498db;
  display: flex;
  align-items: center;
  gap: 5px;
}

.comment-content {
  margin-bottom: 15px;
  padding: 10px;
  background: #f8f9fa;
  border-radius: 6px;
  font-size: 14px;
  line-height: 1.6;
  color: #34495e;
  white-space: pre-wrap;
}

.comment-video {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 15px;
  padding: 10px;
  background: #ecf0f1;
  border-radius: 6px;
}

.video-thumbnail {
  width: 80px;
  height: 45px;
  object-fit: cover;
  border-radius: 4px;
}

.video-title {
  font-size: 14px;
  color: #7f8c8d;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.comment-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.action-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background-color 0.3s;
  display: flex;
  align-items: center;
  gap: 5px;
}

.approve-btn {
  background: #2ecc71;
  color: white;
}

.approve-btn:hover {
  background: #27ae60;
}

.reject-btn {
  background: #e74c3c;
  color: white;
}

.reject-btn:hover {
  background: #c0392b;
}

.delete-btn {
  background: #e74c3c;
  color: white;
}

.delete-btn:hover:not(:disabled) {
  background: #c0392b;
}

.delete-btn:disabled {
  background: #bdc3c7;
  cursor: not-allowed;
  opacity: 0.6;
}

.view-btn {
  background: #3498db;
  color: white;
}

.view-btn:hover {
  background: #2980b9;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #95a5a6;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 15px;
  opacity: 0.5;
}

/* 分页 */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
}

.pagination button {
  padding: 8px 12px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.pagination button:hover:not(:disabled) {
  background: #f8f9fa;
  border-color: #3498db;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  font-size: 14px;
  color: #666;
}

/* 对话框 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
}

.details-dialog {
  max-width: 700px;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.dialog-header h3 {
  margin: 0;
  color: #2c3e50;
}

.close-button {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #95a5a6;
}

.dialog-content {
  padding: 20px;
}

.comment-detail {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.detail-section {
  padding: 15px;
  background: #f8f9fa;
  border-radius: 6px;
}

.detail-section h4 {
  margin: 0 0 15px 0;
  color: #2c3e50;
  font-size: 16px;
}

.detail-item {
  display: flex;
  margin-bottom: 10px;
}

.detail-item:last-child {
  margin-bottom: 0;
}

.detail-label {
  width: 120px;
  font-weight: 500;
  color: #7f8c8d;
}

.detail-value {
  flex: 1;
  color: #34495e;
}

.status-badge {
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.comment-content-full {
  padding: 15px;
  background: white;
  border-radius: 6px;
  line-height: 1.6;
  color: #34495e;
  white-space: pre-wrap;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 30px;
}

.cancel-button {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  background: #ecf0f1;
  color: #7f8c8d;
  transition: background-color 0.3s;
}

.cancel-button:hover {
  background: #bdc3c7;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-filter {
    flex-direction: column;
  }
  
  .filter-group {
    width: 100%;
  }
  
  .filter-group select {
    flex: 1;
  }
  
  .comment-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .comment-meta {
    width: 100%;
    justify-content: space-between;
  }
  
  .comment-actions {
    flex-wrap: wrap;
  }
  
  .action-btn {
    flex: 1;
    justify-content: center;
    min-width: 100px;
  }
  
  .pagination {
    flex-wrap: wrap;
  }
  
  .detail-item {
    flex-direction: column;
    gap: 5px;
  }
}
</style>